package com.permission_server.controller;

import com.permission_server.entity.SysMenuTypeEntity;
import com.permission_server.service.SysMenuTypeService;
import com.permission_server.util.responseUtil.ResponseEnum;
import com.permission_server.util.responseUtil.ResponseUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/menuType")
@Api(tags = "菜单类型管理接口")
public class MenuTypeController {

    @Autowired
    SysMenuTypeService sysMenuTypeService;

    @GetMapping("/pullAllMenuType")
    public ResponseUtil pullAllMenuType() {
        return ResponseUtil.setE(ResponseEnum.OK).setData(sysMenuTypeService.list());
    }

    @PostMapping("/addMenuType")
    public ResponseUtil addMenuType(@RequestBody SysMenuTypeEntity sysMenuType) {
        boolean save = sysMenuTypeService.save(sysMenuType);
        if (save)
            return ResponseUtil.setE(ResponseEnum.OK).setData(sysMenuType).setMsg("菜单【" + sysMenuType.getName() + "】添加成功");
        return ResponseUtil.setE(ResponseEnum.FAIL).setMsg("菜单【" + sysMenuType.getName() + "】添加失败");
    }

    @PostMapping("/delMenuType")
    public ResponseUtil delMenuType(@RequestBody String[] idList) {
        boolean remove = sysMenuTypeService.removeByIds(Arrays.asList(idList));
        if (remove) return ResponseUtil.setE(ResponseEnum.OK).setMsg("有【" + idList.length + "】个菜单删除成功");
        return ResponseUtil.setE(ResponseEnum.FAIL).setMsg("菜单删除失败");
    }

    @PostMapping("/editMenuType")
    public ResponseUtil editMenuType(@RequestBody SysMenuTypeEntity sysMenuType) {
        boolean update = sysMenuTypeService.updateById(sysMenuType);
        if (update) return ResponseUtil.setE(ResponseEnum.OK).setMsg("菜单【" + sysMenuType.getName() + "】更新成功");
        return ResponseUtil.setE(ResponseEnum.FAIL).setMsg("菜单【" + sysMenuType.getName() + "】更新失败");
    }
}
